<?php

namespace App\Http\Controllers\Api;

use App\Repository\Api\GoodsRepository as apiGoods;
use App\Repository\Product\GoodsRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\Product\SetAddressRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class GoodsController extends BaseController
{
    private $model;
    private $addressModel;
    private $productModel;
    private $apiGoodsModel;

    public function __construct(GoodsRepository $GoodsRepository, SetAddressRepository $SetAddressRepository, apiGoods $apiGoods)
    {
        $this->model = $GoodsRepository;
        $this->addressModel = $SetAddressRepository;
        $this->productModel = new ProductRepository();
        $this->apiGoodsModel = $apiGoods;
    }

    public function getGoodsList(Request $request)
    {

        $shop_id = $request->get('shopId');
        if (empty($shop_id)) {
            return $this->returnMsg(100, '参数错误,请确认正确的连接');
        }

        // $shop_id = 1;
        $where = [
            'where' => [
                'shop_id' => $shop_id,
                'is_app' => 0,
                'status' => 0
            ],
            'limit' => [
                'start' => 0,
                'number' => 20
            ],
            'order' => [
                'orderby' => 'sort',
                'orderdir' => 'asc'
            ]
        ];
        $allData = $this->model->index($where);
        $return = [];
        $keys = 10000;
        if (!empty($allData['data'])) {
            foreach ($allData['data'] as $key => $v) {
                $v['surplur'] = mt_rand(1000, 99999);
                $v['sale_nums'] = mt_rand(1, 5);
                if (empty($v['sort'])) {
                    // array_push($allData['data'], $v);
                    // unset($v);
                    $return[$keys] = $v;
                    $keys++;
                }
                if (!empty($v['sort'])) {
                    $return[$key] = $v;
                }
            }
        }
        return $this->returnMsg(200, '查询成功', $return);
        return $allData;
    }

    //获取商品信息
    public function getGoods(Request $request)
    {
        $param = $request->all();
        // var_dump($param);
        $data = $this->model->getOne($param);
        // var_dump($data, 111111);
        if (!$data) {
            return $this->returnMsg(101, '未查询到商品');
        }
        // die();
        if (!empty($data['goods_detail'])) {
            $data['goods_detail'] = htmlspecialchars_decode($data['goods_detail']);
        }
        $goodsNoticeWhere = ['goods_id' => $data['id']];
        if (!empty($data['sup_goods_id_str'])) {
            $arr = explode(',', $data['sup_goods_id_str']);
            if (!empty($arr[1])) {
                $goodsNoticeWhere = ['id' => $arr[1]];
            }
        }
        $list = DB::table('goods_notice')->where($goodsNoticeWhere)->get();
        $data['goods_notice'] = $list;
        // $data['goods_notice_id'] = $goodsNoticeWhere['id'];
        return $this->returnMsg(200, '欢迎下单', $data);
    }

    // 发货地址 传递过来的产品id
    public function getGoodsAddress(Request $request)
    {
        // $where = [];
        // $provinces = DB::table('address_provinces')->where($where)->get();
        // $city = DB::table('address_cities')->where($where)->get();
        // $area = DB::table('address_areas')->where($where)->get();
        // $productId = $request->get('id');
        $goodsId = $request->get('id');
        if (!empty($request->get('product_id'))) {
            $productId = $request->get('product_id');
        }
        if (empty($productId)) {
            $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str']);
            if (!$resGoods) {
                return $this->returnMsg(101, '未查询到商品信息');
            }
            if (!empty($resGoods->sup_goods_id_str)) {
                $prodArr = explode('.', $resGoods->sup_goods_id_str);
                $goodsId = $prodArr[1];
                // return $this->returnMsg(102, '未查询号码信息');
            }

        }
        $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'product_id']);
        if (empty($resGoods->product_id)) {
            return $this->returnMsg(103, '未查询到产品');
        }
        $productId = $resGoods->product_id;
        // DB::table()->
        $wheres = [
            'id' => $productId,
        ];
        $Product = $this->productModel->getOne($wheres, ['id', 'product_address']);
        if (!$Product) {
            // 没查询到配置的话 直接全国
            return $this->returnMsg(100, '未查询到产品信息',);
        }
        // var_dump($Product);
        // 通过产品id 然后查询到归属地
        $where = [
            'id' => $Product->product_address
        ];
        $addressArr = $this->addressModel->getOne($where, ['id', 'name', 'type', 'province_city']);
        // if (empty($addressArr->province_city)) {
        //   return $this->returnMsg(100, '未查询到收货地');
        // }

        // 查询地址
        $province = [];
        $city = [];
        if (!empty($addressArr->province_city)) {
            $arr = json_decode($addressArr->province_city, true);

            foreach ($arr as $key => $v) {
                $province[] = $v[0];
                if (!empty($v[1])) {
                    $city[] = $v[1];
                }
            }
        }

        return $this->apiGoodsModel->getAddress($province, $city, true, !empty($addressArr->type) ? $addressArr->type : 0);
        // return $this->getAddress([], []);
    }

    // 获取号码的归属地  传递过来的是产品id
    public function getOwnerAddress(Request $request)
    {
        $goodsId = $request->get('id');
        if (!empty($request->get('product_id'))) {
            $productId = $request->get('product_id');
        }
        if (empty($productId)) {
            $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str', 'product_id']);
            if (!$resGoods) {
                return $this->returnMsg(101, '未查询到商品信息');
            }
            if (!empty($resGoods->product_id)) {
                $productId = $resGoods->product_id;
            }

            if (!empty($resGoods->sup_goods_id_str)) {
                $prodArr = explode('.', $resGoods->sup_goods_id_str);
                $goodsId = $prodArr[1];
                $resGoodss = $this->model->getOne(['id' => $goodsId], ['id', 'product_id']);
                if (!$resGoodss) {
                    return $this->returnMsg(101, '未查询到商品信息');
                }
                if (!empty($resGoodss->product_id)) {
                    $productId = $resGoodss->product_id;
                }
            }
            if (empty($productId)) {
                return $this->returnMsg(102, '未查询号码归属地');
            }
        }
        // DB::table()->
        $wheres = [
            'id' => $productId,
        ];
        $Product = $this->productModel->getOne($wheres, ['id', 'product_address']);
        if (!$Product) {
            return $this->returnMsg(100, '未查询到产品信息');
        }
        // var_dump($Product);
        // 通过产品id 然后查询到归属地
        $where = [
            'id' => $Product->product_address
        ];
        $addressArr = $this->addressModel->getOne($where, ['id', 'name', 'owner']);
        if (empty($addressArr->owner)) {
            return $this->returnMsg(100, '未查询到归属地');
        }
        // 查询地址
        $arr = json_decode($addressArr->owner, true);
        $province = [];
        $city = [];
        foreach ($arr as $key => $v) {
            $province[] = $v[0];
            $city[] = $v[1];
        }
        return $this->apiGoodsModel->getAddress($province, $city, false);
        // return $this->returnMsg(200, '查询成功', json_decode($addressArr->owner));
    }

    // 获取号码
    public function getSelectNumber(Request $request)
    {
        $param = $request->all();
        $goodsId = $request->get('id');
        if (!empty($request->get('product_id'))) {
            $productId = $request->get('product_id');
        }
        if (empty($productId)) {
            $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str']);
            if (!$resGoods) {
                return $this->returnMsg(101, '未查询到商品信息');
            }
            if (empty($resGoods->sup_goods_id_str)) {
                return $this->returnMsg(102, '未查询号码信息');
            }
            $prodArr = explode('.', $resGoods->sup_goods_id_str);
            $goodsId = $prodArr[1];
        }
        $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'product_id']);
        if (empty($resGoods->product_id)) {
            return $this->returnMsg(102, '未查询到产品');
        }
        $productId = $resGoods->product_id;
        // $productArr = $this->productModel->getOne($wheres, ['id', 'channel_id']);
        // $data = 'numbercity';
        $data = $this->apiGoodsModel->getSelectNumber($productId, $param);
        return $data;
        // if (empty($data)) {
        //   return $this->returnMsg(100, '查询失败', []);
        // }
        // // $datas = json_decode($data, true);
        // return $this->returnMsg(200, '查询成功', $data);
    }

    //  获取号码2
    public function getSelectNumberTwo(Request $request)
    {
        $param = $request->all();
        $goodsId = $request->get('id');
        if (!empty($request->get('product_id'))) {
            $productId = $request->get('product_id');
        }
        if (empty($productId)) {
            $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str']);
            if (!$resGoods) {
                return $this->returnMsg(101, '未查询到商品信息');
            }
            if (empty($resGoods->sup_goods_id_str)) {
                return $this->returnMsg(102, '未查询号码信息');
            }
            $prodArr = explode('.', $resGoods->sup_goods_id_str);
            $goodsId = $prodArr[1];
        }
        $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'product_id']);
        if (empty($resGoods->product_id)) {
            return $this->returnMsg(102, '未查询到产品');
        }
        $productId = $resGoods->product_id;
        // $productArr = $this->productModel->getOne($wheres, ['id', 'channel_id']);
        // $data = 'numbercity';
//        print_r($param);
        $data = $this->apiGoodsModel->getSelectNumberTwo($productId, $param);
        return $data;
        // if (empty($data)) {
        //   return $this->returnMsg(100, '查询失败', []);
        // }
        // // $datas = json_decode($data, true);
        // return $this->returnMsg(200, '查询成功', $data);
    }
    // 查询通道的号码
    // 查询 号码
    public function getKefuPhone(Request $request)
    {
        $shop_id = $request->get('shop_id');
        $return = DB::table('kefu_config')->where(['shop_id' => $shop_id])->get();
        if ($return) {
            $datas = [];
            foreach ($return as $key => $v) {
                $datas[$v->name] = $v->value;
            }
            // var_dump($datas);
            if (!empty($datas)) {
                return $this->returnMsg(200, '查询成功', ['kefu_phone' => !empty($datas['kefu_phone']) ? 'tel:' . $datas['kefu_phone'] : '', 'kefu_url' => !empty($datas['kefu_url']) ? $datas['kefu_url'] : '']);
            }
            return $this->returnMsg(200, '请联系商家添加客服信息');
        }
        return $this->returnmsg(200, '查询成功', ['kefu_phone' => 'tel:' . '4006410001', 'kefu_url' => 'https://work.weixin.qq.com/kfid/kfc848b57a38f3e4cb8']);
    }
}
